package com.kamistoat.meimeimall.meimeimallproduct.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.annotation.LoginRequired;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.meimeimallproduct.entity.CategoryEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kamistoat.meimeimall.meimeimallproduct.service.CategoryService;
import com.kamistoat.meimeimall.common.utils.R;


/**
 * 商品三级分类
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 15:16:42
 */
@Api(tags = "商品分类数据库操作")
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @ApiOperation("从数据库获取列表")
    @GetMapping("/list")
    @DubboAndHTTPExceptionCatch
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 查询单条数据信息。这里就是查询单条菜单信息
     */
    @ApiOperation("从数据库查询指定POJO")
    @GetMapping("/info/{categoryId}")
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:category:info")
    public R info(@PathVariable("categoryId") Long categoryId) {
        CategoryEntity category = categoryService.getById(categoryId);
        return R.ok().put("singlecategory", category);
    }

    /**
     * 保存
     */
    @ApiOperation("向数据库保存POJO")
    @PutMapping("/")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 批量修改，主要用于对拖拽数据的修改。由于主要修改排序值，因此起名为sort
     *
     * @param category 前端发送来的一个json文件，是由一个列表封装而成，列表的元素就是CategoryEntity
     */
    @ApiOperation("批量修改商品分类的顺序")
    @PostMapping("/category_sort_multi_update")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category) {
        // 自动生成的Dao中具有按列表修改的功能
        // 接收collection类输入，使用Array.toList将列表转换成collection
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("向数据库更新POJO")
    @PostMapping("/category_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("从数据库删除POJO")
    @DeleteMapping("/category_info")
    @LoginRequired
    @DubboAndHTTPExceptionCatch
    //@RequiresPermissions("meimeimallproduct:category:delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
